package project.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Users.Client;
import schronisko.Queries;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	Queries q = new Queries();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("Password");
		HttpSession s = request.getSession();
		response.setContentType("text/html");
		int valid = q.ValidateClient(username, password);
		switch(valid) {
		case 0:
			s.setAttribute("message","Problem z baza. Sprobuj ponownie");
			response.sendRedirect("login.jsp");
			break;
		case -1:
			s.setAttribute("message","Brak uzytkownika o podanej nazwie");
			response.sendRedirect("login.jsp");
			break;
		case -2:
			s.setAttribute("message","Niepoprawne haslo!");
			response.sendRedirect("login.jsp");
			break;
		case -3:
			s.setAttribute("message","Konto nieaktywne. Sprawdz skrzynke");
			response.sendRedirect("login.jsp");
			break;
		default:
			

			s.setAttribute("message_s","Logowanie poprawne<br>Za 3 sekundy zostaniesz przekierowany do panelu.<br>Jesli nie nastapi, kliknij <a href='Panel/Panel.jsp'>tutaj</a>");
			s.setAttribute("ID", valid);
			s.setAttribute("username", username);
			int type = q.getPrivilidge(valid);
			s.setAttribute("type", type);
			s.setAttribute("temp", 0);
			s.setAttribute("changed_pass", 0);
			s.setAttribute("edited", 0);
			Client c = q.getClient(valid);
			s.setAttribute("client_data", c);
			s.setAttribute("step", 0);
			s.setAttribute("step_a", 0);
			s.setAttribute("transaction", 0);
			s.setAttribute("worker_list",q.getWorkers());
			s.setAttribute("detailed_w_list", q.getDetailedWorkers());
			s.setAttribute("added", 0);
			s.setAttribute("session_id", s.getId());
			s.setAttribute("adding", 0);
			s.setAttribute("deleted", 0);
			s.setAttribute("sent", 0);
			s.setAttribute("msg_req", 0);
			s.setAttribute("cleaned", 0);
			s.setAttribute("deleted_anim", 0);
			s.setAttribute("deleted_user", 0);
			s.setAttribute("adopt_step", 0);
			s.setAttribute("accept", 0);
			s.setAttribute("decline", 0);
			response.sendRedirect("login.jsp");
			break;
		}

	}
	public void destroy() {
		
	}

}
